package com.dsiab.manageService.service.impl;

import com.dsiab.manageService.entity.WebStatsDto;
import com.dsiab.manageService.mapper.WebStatsDao;
import com.dsiab.manageService.service.WebStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class WebStatsServiceImpl implements WebStatsService {

  @Autowired
  WebStatsDao webStatsDao;


  public void insertWebStats(WebStatsDto webStatsDto) {
    webStatsDao.insertWebStats(webStatsDto);
  }

  public void insertEvent(WebStatsDto webStatsDto) {
    webStatsDao.insertEvent(webStatsDto);
  }

  public Map getWebStatistics(String type) {
    // 获取当天时间
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    // 获取前当天时间
    Calendar calendarCurr = new GregorianCalendar();
    calendarCurr.setTime(new Date());
    calendarCurr.add(Calendar.DATE,0);
    String currDate = sdf.format(calendarCurr.getTime());

    // 获取前一天时间
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(new Date());
    calendar.add(Calendar.DATE,-1);
    String lastDate = sdf.format(calendar.getTime());

    // 获取前七天时间
    Calendar calendar7days = new GregorianCalendar();
    calendar7days.setTime(new Date());
    calendar7days.add(Calendar.DATE,-6);
    String last7daysDate = sdf.format(calendar7days.getTime());

    // 获取前30天时间
    Calendar calendar30days = new GregorianCalendar();
    calendar30days.setTime(new Date());
    calendar30days.add(Calendar.DATE,-29);
    String last30daysDate = sdf.format(calendar30days.getTime());

    String endDate = "";

    if (type.isEmpty() | type.equals("toDay") ){
      endDate = currDate;
    } else if (type.equals("yesterday")){
      endDate = lastDate;
    }else if (type.equals("7day")){
      endDate = last7daysDate;
    }else if (type.equals("30day")){
      endDate = last30daysDate;
    }

    Map map = new HashMap();
    // 查所有访问量
    map.put("allViews", webStatsDao.getCountViews());

    Map currDayViews = webStatsDao.getSomeDayViews(currDate);
    Map lastDayViews = webStatsDao.getSomeDayViews(lastDate);

    // 访问量
    int twoDayagoNum = (int)currDayViews.get("allViews") - (int)lastDayViews.get("allViews") ;
    map.put("allViewsMom", twoDayagoNum);
    int twoDayagoNum2 =(int)currDayViews.get("allpages") - (int)lastDayViews.get("allpages");
    map.put("allpagesMom", twoDayagoNum2);
    int twoDayagoNum3 =  (int)currDayViews.get("dayViews") - (int)lastDayViews.get("dayViews");
    map.put("dayViewsMom",twoDayagoNum3);
    int twoDayagoNum4 = (int)currDayViews.get("dayIp") - (int)lastDayViews.get("dayIp");
    map.put("dayIpMom",twoDayagoNum4);


    // 查当天Ip
    map.put("dayIp",webStatsDao.getCurrentViewsByIp());
    // 查当天数据
    map.put("dayViews",webStatsDao.getCurrentViewsByDate());
    // 查所有文章
    map.put("allpages", webStatsDao.getAllCount());

    // 查所有设备类型
    map.put("deviceType", webStatsDao.getDeviceType(currDate,endDate));
    // 查所有分辨率
    map.put("deviceRatio", webStatsDao.getDeviceRatio(currDate,endDate));
    // 查所有浏览器类型
    map.put("browserType", webStatsDao.getBrowserType(currDate,endDate));
    //  查看每天浏览量
    map.put("everyDayViews", webStatsDao.getEveryDayViews());
    // 搜索引擎
    // map.put("botEngine", webStatsDao.botEngine());
    // 查所当天访问来源
    map.put("referrer", currDayViews.get("referrer"));
    // 查所当天访问地址数
    map.put("postView", currDayViews.get("viewAddress"));
    List<Map> mapList = webStatsDao.getMapData(currDate,endDate);
    List <Map>cityList = new ArrayList<>();
    List <Map>countryList = new ArrayList<>();
    for (Map city: mapList){
      String cname = (String) city.get("region");
      String[] cityArryr = cname.split("\\|");
      String cityName = cityArryr[2];
      String countryName = cityArryr[0];
      // 中国省市数据
      if ("中国".equals(countryName)){
        // 过滤为空的数据
        if (!cityName.equals("0")) {
          Map cmap = new HashMap<>();
          cmap.put("name",cityName);
          cmap.put("value",city.get("value"));
          cityList.add(cmap);
        }
      }
      // 国家数据
      Map countryNameMap = new HashMap<>();
      countryNameMap.put("name",countryName);
      countryNameMap.put("value",city.get("value"));
      countryList.add(countryNameMap);

    }
    map.put("mapData", cityList);
    map.put("worldMapData", countryList);
    // 查询开发笔记跟生活笔记总字数
    // map.put("totalWordsNum", webStatsDao.getTotalWordsNumByCate());
    return map;
  }
}